package org.nf.trading.factory;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.properties.MinioProperties;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * @author wangl
 * @date 2023/10/26
 * 整合MinioClient
 */
@Data
@Slf4j
public class MinioFactoryBean implements FactoryBean<MinioClient> {

    @Override
    public MinioClient getObject() throws Exception {
        MinioClient client = MinioClient.builder()
                .endpoint(MinioProperties.URL)
                .credentials(MinioProperties.USER,MinioProperties.PASSWORD)
                .build();
        //初始化桶
        initBucket(client);
        log.info("已初始化桶：" + MinioProperties.BUCKET);
        return client;
    }

    @Override
    public Class<?> getObjectType() {
        return MinioClient.class;
    }

    private void initBucket(MinioClient client) throws Exception{
        if(!client.bucketExists(BucketExistsArgs.builder()
                .bucket(MinioProperties.BUCKET).build())) {
            client.makeBucket(MakeBucketArgs.builder()
                    .bucket(MinioProperties.BUCKET).build());
        }
    }
}